package com.ibm.cps.processors.vector;

import com.ibm.cps.sql.expression.CalculationNode;
import com.ibm.util.ErrorCode;
import com.ibm.util.exception.CPSException;

public class OperatorFunctions {
	private OperatorFunctions(){}
	
	public static OperatorFunction getOperatorFunction(String operator) throws CPSException{
		// accordint to CalculationNode.Operator, change this if statement.
		if(operator.equals("PLUS")){
			return new Plus();
		}else if(operator.equals("MINUS")){
			return new Minus();
		}else if(operator.equals("TIMES")){
			return new Multiply();
		}else if(operator.equals("DIVIDE")){
			return new Divide();
		}else{
			throw new CPSException(ErrorCode.INVALID_INPUT_ERROR, "Unexpected operator " + operator);
		}
	}
	
	private static class Plus implements OperatorFunction{

		@Override
		public double evaluate(double leftValue, double rightValue) {
			return leftValue + rightValue;
		}
		
	}
	
	private static class Minus implements OperatorFunction{

		@Override
		public double evaluate(double leftValue, double rightValue) {
			return leftValue - rightValue;
		}
		
	}
	
	private static class Multiply implements OperatorFunction{

		@Override
		public double evaluate(double leftValue, double rightValue) {
			return leftValue * rightValue;
		}
		
	}
	
	private static class Divide implements OperatorFunction{

		@Override
		public double evaluate(double leftValue, double rightValue) throws CPSException {
			if (rightValue == 0) {
				throw new CPSException(ErrorCode.INVALID_INPUT_ERROR, "can not divide, the value is zero");
			}
			return leftValue / rightValue;
		}
		
	}
}
